﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PsychveyApp1.classes;
using System.Collections;

namespace PsychveyApp1
{
    public partial class InformationSheet : System.Web.UI.Page
    {
        public TextBox tbxInfoSheet;
        public TextBox tbxInfoConsentForm;
        public ArrayList conditionTextBoxArray;
        public ArrayList removeConditionCheckBoxArray;
        public ArrayList listOfConditions;
        public TextBox tbxConditions;
        TableRow row = new TableRow();
        TableCell cell = new TableCell();


        protected void Page_Load(object sender, EventArgs e)
        {
            Survey currentSurvey = (Survey)Session["currentSurvey"];
            lblSurveyID.Text = currentSurvey.surveyID.ToString();

            //lblDate.Text = "Date: " + DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year + "-" + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();

            if (!IsPostBack)
            {
                ArrayList infoSheet = DBManager.GetInformationConsentAndSheetBySurveyID(((Survey)Session["currentSurvey"]).surveyID);
                //((InformationConsentAndSheet)infoSheet).imageURL
                if (infoSheet.Count == 1)
                {
                    if (!((InformationConsentAndSheet)infoSheet[0]).imageURL.Equals(""))
                        imgShow.ImageUrl = DBManager.GetImageURL(currentSurvey.surveyID);

                    TextBox1.Text = DBManager.GetInformationSheetText(currentSurvey.surveyID);
                    TextBox2.Text = DBManager.GetInformationConsentForm(currentSurvey.surveyID);



                    //tbxName.Text = DBManager.GetName(currentSurvey.surveyID);
                    //tbxDate.Text = DBManager.GetDate(currentSurvey.surveyID);

                }
            }
           
         
               this.conditionTextBoxArray = new ArrayList();
               this.removeConditionCheckBoxArray = new ArrayList(); 
               this.listOfConditions = DBManager.GetListOfConditions(currentSurvey.surveyID);
               for (int j = 0; j < listOfConditions.Count; j++)
               {
                   row = new TableRow();
                   cell = new TableCell();
                   tbxConditions = new TextBox();
                   tbxConditions.Width = 400;
                   tbxConditions.Text = ((InformationConditionConsent)listOfConditions[j]).checkboxText;
                   this.conditionTextBoxArray.Add(tbxConditions);
                   cell.Controls.Add(tbxConditions);
                   row.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Justify;
                   row.Cells.Add(cell);

                   cell = new TableCell();
                   cell.ColumnSpan = 1;
                   CheckBox cbxConditionCheckbox = new CheckBox();
                   this.removeConditionCheckBoxArray.Add(cbxConditionCheckbox);
                   cell.Controls.Add(cbxConditionCheckbox);
                   row.Cells.Add(cell);
                   textboxTable2.Rows.Add(row);
               }
               this.Panel2.Controls.Add(textboxTable2);


              
        }

        protected void btnProceedSurvey_Click(object sender, EventArgs e)
        {
            ArrayList listOfConditions = DBManager.GetListOfConditions(((Survey)Session["currentSurvey"]).surveyID);
              
            SaveAll();
            Server.Transfer("CreateSurvey2.aspx?" + Session["surveyid"]);
               
         }
           

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
                if (extension == ".jpg")
                {
                    string ImgPath = Server.MapPath("~/images/" + Guid.NewGuid() + FileUpload1.FileName);
                    FileUpload1.SaveAs(ImgPath);
                    string ShowImgPath = ImgPath.Substring(ImgPath.LastIndexOf("\\"));
                    imgShow.ImageUrl = "~/images" + ShowImgPath;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Image uploaded');", true);
                }
                else if (extension == ".png")
                {
                    string ImgPath = Server.MapPath("~/images/" + Guid.NewGuid() + FileUpload1.FileName);
                    FileUpload1.SaveAs(ImgPath);
                    string ShowImgPath = ImgPath.Substring(ImgPath.LastIndexOf("\\"));
                    imgShow.ImageUrl = "~/images" + ShowImgPath;
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Image uploaded');", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Please select the image to upload');", true);
            }
        }

        protected void btnAddTextBox_Click(object sender, EventArgs e)
        {
            SaveAll();
            conditionTextBoxArray = new ArrayList();
            ArrayList listOfConditions = DBManager.GetListOfConditions(((Survey)Session["currentSurvey"]).surveyID);

            CurrentMaxIDNumber currentMaxIDNumber = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
            currentMaxIDNumber.currentMaxConditionID++;
            InformationConditionConsent infoConsentCondition = new InformationConditionConsent();
            infoConsentCondition.surveyID = (((Survey)Session["currentSurvey"]).surveyID);
            infoConsentCondition.checkboxText = "";
            infoConsentCondition.conditionID = currentMaxIDNumber.currentMaxConditionID.ToString();
            DBManager.InsertConditions(infoConsentCondition, ((Survey)Session["currentSurvey"]).surveyID);
            DBManager.UpdateCurrentMaxIDNumber(currentMaxIDNumber);
            Response.Redirect("InformationSheet.aspx");
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        public void SaveAll()
        {
            ArrayList result = DBManager.GetInformationConsentAndSheetBySurveyID(((Survey)Session["currentSurvey"]).surveyID);
            if (result.Count == 1)
            {
                DBManager.UpdateInformationConsentAndSheet((((Survey)Session["currentSurvey"]).surveyID),imgShow.ImageUrl.ToString(), TextBox1.Text, TextBox2.Text);
                for (int k = 0; k < conditionTextBoxArray.Count ; k++)
                {
                    DBManager.UpdateInformationConditionConsent((((Survey)Session["currentSurvey"]).surveyID), ((InformationConditionConsent)listOfConditions[k]).conditionID, ((TextBox)conditionTextBoxArray[k]).Text);
                }
            }
            else
            {
                InformationConsentAndSheet infoConsentAndSheet = new InformationConsentAndSheet();
                infoConsentAndSheet.surveyID = (((Survey)Session["currentSurvey"]).surveyID);
                infoConsentAndSheet.imageURL = imgShow.ImageUrl.ToString();
                infoConsentAndSheet.informationSheetText = TextBox1.Text;
                infoConsentAndSheet.informationConsentForm = TextBox2.Text;
                DBManager.InsertInformationConditionAndSheet(infoConsentAndSheet);

                for (int k = 0; k < conditionTextBoxArray.Count; k++)
                {
                    DBManager.UpdateInformationConditionConsent((((Survey)Session["currentSurvey"]).surveyID), ((InformationConditionConsent)listOfConditions[k]).conditionID, ((TextBox)conditionTextBoxArray[k]).Text);
                }
            }
        }

        protected void btnDeleteCondition_Click(object sender, EventArgs e)
        {
            for (int c = 0; c < this.removeConditionCheckBoxArray.Count; c++)
            {
                if (((CheckBox)this.removeConditionCheckBoxArray[c]).Checked == true)
                {
                    InformationConditionConsent infoConditionConsent = (InformationConditionConsent)this.listOfConditions[c];
                    DBManager.DeleteInformationConditionConsent(((InformationConditionConsent)this.listOfConditions[c]).surveyID, ((InformationConditionConsent)this.listOfConditions[c]).conditionID);

                }

            }
            Response.Redirect("InformationSheet.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }

     
    }
}